/*
 * @lc app=leetcode.cn id=98 lang=javascript
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */

// var isValidBST = function(root) {
//   if(!root) return true;
//   let last = -Infinity

//   var inorder = function(root) {
//     if(!root) return true;
//     if(!inorder(root.left)) return false;
  
//     if(root.val <= last) return false;
//     last = root.val;
  
//     if(!inorder(root.right)) return false;
//     return true;
//   }
//   return inorder(root);
// };

/**
 * 复习 2021-08-04
 */
var isValidBST = function(root) {
  if(!root) return true;
  let pre = null;
  var inOrder = function (root){
    if(!root) return true;
    if(!inOrder(root.left)) return false;

    if(pre != null && (root.val <= pre)) return false;
    pre = root.val;
    
    if(!inOrder(root.right)) return false;
    return true;
  }
  
  return inOrder(root);
}
// @lc code=end

